package Eyjafjalla.patch;

import Eyjafjalla.card.AbstractEyjafjallaCard;
import Eyjafjalla.modifier.FireMarkMod;
import Eyjafjalla.power.DancingLavaPower;
import Eyjafjalla.power.VolcanicHealingPower;
import Eyjafjalla.relic.ShadowOfPompeii;
import Eyjafjalla.relic.VolcanoMask;
import basemod.helpers.CardModifierManager;
import com.evacipated.cardcrawl.modthespire.lib.SpirePatch;
import com.evacipated.cardcrawl.modthespire.lib.SpirePostfixPatch;
import com.evacipated.cardcrawl.modthespire.lib.SpirePrefixPatch;
import com.evacipated.cardcrawl.modthespire.lib.SpireReturn;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.DamageInfo;
import com.megacrit.cardcrawl.cards.DescriptionLine;
import com.megacrit.cardcrawl.cards.SoulGroup;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.powers.AbstractPower;
import com.megacrit.cardcrawl.relics.AbstractRelic;
import com.megacrit.cardcrawl.rooms.AbstractRoom;
import com.megacrit.cardcrawl.ui.panels.EnergyPanel;
import com.megacrit.cardcrawl.vfx.FastCardObtainEffect;
import com.megacrit.cardcrawl.vfx.cardManip.PurgeCardEffect;

public class OtherPatch {

    @SpirePatch(clz = SoulGroup.class,method = "obtain")
    public static class SoulObtainPatch{
        @SpirePostfixPatch
        public static void Postfix(SoulGroup _inst, AbstractCard card, boolean obtainCard){
            if(obtainCard&&card instanceof AbstractEyjafjallaCard){
                AbstractCard removed = ((AbstractEyjafjallaCard) card).removeCardWhenObtained();
                if(removed!=null){
                    if(removed.upgraded){
                        card.upgrade();
                    }
                    if(CardModifierManager.hasModifier(removed, FireMarkMod.ID)){
                        CardModifierManager.addModifier(card,new FireMarkMod());
                    }
                    CardCrawlGame.sound.play("CARD_EXHAUST");
                    AbstractDungeon.topLevelEffectsQueue.add(new PurgeCardEffect(removed, (float)(Settings.WIDTH / 2), (float)(Settings.HEIGHT / 2)));
                    AbstractDungeon.player.masterDeck.removeCard(removed);
                }
            }
        }
    }

    //火舞之人伤害转移
    @SpirePatch(clz = AbstractPlayer.class,method = "damage")
    public static class DancingLavaPatch {
        @SpirePrefixPatch
        public static SpireReturn<Void> Prefix(AbstractPlayer _inst, DamageInfo info) {
            if (AbstractDungeon.getCurrRoom().phase.equals(AbstractRoom.RoomPhase.COMBAT)) {
                AbstractPower heal = _inst.getPower(VolcanicHealingPower.POWER_ID);
                if (heal != null) {
                    heal.onSpecificTrigger();
                    int block = info.output/(info.type.equals(DamageInfo.DamageType.NORMAL)?5:2);
                    if(block>0)
                        _inst.addBlock(block);
                }
                if (!info.type.equals(DamageInfo.DamageType.NORMAL)) {
                    AbstractPower lava = _inst.getPower(DancingLavaPower.POWER_ID);
                    if (lava != null) {
                        lava.onSpecificTrigger();
                        AbstractMonster monster = AbstractDungeon.getRandomMonster();
                        if (monster != null)
                            monster.damage(info);
                        return SpireReturn.Return();
                    }
                }
            }
            return SpireReturn.Continue();
        }
    }

    //能量
    @SpirePatch(clz = EnergyPanel.class,method = "useEnergy")
    public static class UseEnergyPatch{
        @SpirePostfixPatch
        public static void Postfix(int e){
            if(e>0&&EnergyPanel.getCurrentEnergy()==0){
                AbstractRelic vm = AbstractDungeon.player.getRelic(VolcanoMask.ID);
                if(vm!=null){
                    vm.onTrigger();
                }
                AbstractRelic sp = AbstractDungeon.player.getRelic(ShadowOfPompeii.ID);
                if(sp!=null){
                    sp.onTrigger();
                }
            }
        }
    }

}
